package cn.iocoder.yudao.module.shop.controller.admin.handlestrategyuser;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.shop.dal.dataobject.handlestrategy.ShopHandleStrategyDO;
import cn.iocoder.yudao.module.shop.service.handlestrategy.ShopHandleStrategyService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.handlestrategyuser.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.handlestrategyuser.ShopHandleStrategyUserDO;
import cn.iocoder.yudao.module.shop.convert.handlestrategyuser.ShopHandleStrategyUserConvert;
import cn.iocoder.yudao.module.shop.service.handlestrategyuser.ShopHandleStrategyUserService;

@Tag(name = "管理后台 - 艾灸师绑定的工资策略")
@RestController
@RequestMapping("/shop/handle-strategy-user")
@Validated
public class ShopHandleStrategyUserController {

    @Resource
    private ShopHandleStrategyUserService handleStrategyUserService;


    @Resource
    private ShopHandleStrategyService handleStrategyService;

    @PostMapping("/create")
    @Operation(summary = "创建艾灸师绑定的工资策略")
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy-user:create')")
    public CommonResult<Long> createHandleStrategyUser(@Valid @RequestBody ShopHandleStrategyUserCreateReqVO createReqVO) {
        return success(handleStrategyUserService.createHandleStrategyUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新艾灸师绑定的工资策略")
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy-user:update')")
    public CommonResult<Boolean> updateHandleStrategyUser(@Valid @RequestBody ShopHandleStrategyUserUpdateReqVO updateReqVO) {
        handleStrategyUserService.updateHandleStrategyUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除艾灸师绑定的工资策略")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy-user:delete')")
    public CommonResult<Boolean> deleteHandleStrategyUser(@RequestParam("id") Long id) {
        handleStrategyUserService.deleteHandleStrategyUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得艾灸师绑定的工资策略")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy-user:query')")
    public CommonResult<ShopHandleStrategyUserRespVO> getHandleStrategyUser(@RequestParam("id") Long id) {
        ShopHandleStrategyUserDO handleStrategyUser = handleStrategyUserService.getHandleStrategyUser(id);
        return success(ShopHandleStrategyUserConvert.INSTANCE.convert(handleStrategyUser));
    }

    @GetMapping("/list")
    @Operation(summary = "获得艾灸师绑定的工资策略列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy-user:query')")
    public CommonResult<List<ShopHandleStrategyUserRespVO>> getHandleStrategyUserList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopHandleStrategyUserDO> list = handleStrategyUserService.getHandleStrategyUserList(ids);
        return success(ShopHandleStrategyUserConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得艾灸师绑定的工资策略分页")
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy-user:query')")
    public CommonResult<PageResult<ShopHandleStrategyUserRespVO>> getHandleStrategyUserPage(@Valid ShopHandleStrategyUserPageReqVO pageVO) {
        PageResult<ShopHandleStrategyUserDO> pageResult = handleStrategyUserService.getHandleStrategyUserPage(pageVO);
        List<Long> strategyIdList = CollectionUtils.convertList(pageResult.getList(), ShopHandleStrategyUserDO::getStrategyId);
        List<ShopHandleStrategyDO> strategyList = handleStrategyService.getHandleStrategyList(strategyIdList);
        Map<Long, ShopHandleStrategyDO> strategyDOMap = CollectionUtils.convertMap(strategyList, ShopHandleStrategyDO::getId);

        PageResult<ShopHandleStrategyUserRespVO> result = ShopHandleStrategyUserConvert.INSTANCE.convertPage(pageResult);
        for(int i = 0; i<result.getList().size(); i++){
            ShopHandleStrategyUserRespVO item = result.getList().get(i);
            item.setStrategy(strategyDOMap.get(item.getStrategyId()));
        }

        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出艾灸师绑定的工资策略 Excel")
    @PreAuthorize("@ss.hasPermission('shop:handle-strategy-user:export')")
    @OperateLog(type = EXPORT)
    public void exportHandleStrategyUserExcel(@Valid ShopHandleStrategyUserExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopHandleStrategyUserDO> list = handleStrategyUserService.getHandleStrategyUserList(exportReqVO);
        // 导出 Excel
        List<ShopHandleStrategyUserExcelVO> datas = ShopHandleStrategyUserConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "艾灸师绑定的工资策略.xls", "数据", ShopHandleStrategyUserExcelVO.class, datas);
    }

}
